/* Copyright (C) 2008  Romolo Camplani, Marco Fortunato, Marco Marelli, Guido Rota, Fabio A. Schreiber et al.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
/*
 * ModelDescriptionBuilder.java
 *
 * Created on October 30, 2007, 7:28 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.dei.perla.utils.emulator.runtime;
import java.util.*;

import org.dei.perla.utils.emulator.model.node.Node;
import org.dei.perla.utils.emulator.models.world.Position;
/**
 *
 * @author Adnan
 */
public class ModelDescriptionBuilder implements InputInterfaceModelDescriptionBuilder, SimulationPerformerMDB{
    
    ArrayList<Node> nodes;
    InputInterface ii;
    boolean defaultNodeExists;
    static int i=0;
    /**
     * Creates a new instance of ModelDescriptionBuilder
     */
    public ModelDescriptionBuilder() {
       
        nodes = new ArrayList<Node>();
        defaultNodeExists = false;
    }
    public ModelDescriptionBuilder(ArrayList<Node> nodes)
    {
        this.nodes = nodes;
    }
    
    
    
    public ArrayList<Node> go()
    {
        ii = new InputInterface();
        nodes = ii.go();
       // showNodes();
        return nodes;
        
    }
    
    public void createNode() // creating a node with default value
    {
       
        for(Node tempnode:nodes)
                if (tempnode.getID().equals("1"))
                {
                    defaultNodeExists = true;
                }
        
        if (!defaultNodeExists)
        {
            nodes.add(new Node("1","-1",new Position(5,5),5.0));
            System.out.println("Node created successfully");
            defaultNodeExists = true;
        }
        else System.out.println("A node with same Id already exists, node creation failed.");
    }
    
    public void createNode(String nodeID, String baseID, int posX, int posY, double charge) // creating a node with default value
    {
        for (Node tnode:nodes)
            if (tnode.getID().equals(nodeID))
            {
                System.out.println("A node with same Id already exists. Node creating failed.");
                return;
            }
        
        nodes.add(new Node(nodeID,baseID,new Position(posX,posY),charge));
        System.out.println("Node created successfully");
    }
    
    public void showNode(String nodeID)
    {
        for (Node tnode:nodes)
            if (tnode.getID().equals(nodeID))
                tnode.displayData();
    }
    
    public void showNodes()
    {
        System.out.println("Displaying info for all the nodes");
        for (Node tnode:nodes)
                tnode.displayData();
         
    }
        
    public void getData(ArrayList<String> tquery)
    {
        for (Node tnode:nodes)
            if (tnode.getID().equals(tquery.get(2)))
                tnode.getData("Charge");
    }
    
     public void sendData(ArrayList<String> tquery)
    {
        for (Node tnode:nodes)
            if (tnode.getID().equals(tquery.get(2)))
                tnode.sendData("Charge"+" "+tquery.get(3));
    }
     
     public ArrayList<Node> getNodes()
     {
          ArrayList<Node> nodeList = new ArrayList<Node>();
            for (Node tnode:nodes)
            {
                    double charge = Double.parseDouble(tnode.getCharge());
                    if (charge > 0) // MDB should contain only nodes that have positive charge
                        nodeList.add(tnode);
            }
            nodes = nodeList;
            return nodes;
     }
     
     public ArrayList<Node> getLivingNodes(ArrayList<Node> nodeList)
     {
          ArrayList<Node> nodeList2 = new ArrayList<Node>();
            for (Node tnode:nodeList)
            {
                    double charge = Double.parseDouble(tnode.getCharge());
                    if (charge > 0) // MDB should contain only nodes that have positive charge
                        nodeList2.add(tnode);
            }
            //nodes = nodeList;
            return nodeList2;
     }
      public void setNodes(ArrayList<Node> tnodes)
      {
          
            ArrayList<Node> nodeList = new ArrayList<Node>();
            for (Node tnode:tnodes)
            {
                    double charge = Double.parseDouble(tnode.getCharge());
                    if (charge > 0) // MDB should contain only nodes that have positive charge
                        nodeList.add(tnode);
            }
            nodes = nodeList;
            
            //nodes = tnodes; 
      }
}
